// C Source File
// Created 11/19/2007; 7:32:53 PM

#include "Civilization.h"
#include "Terrain.h"
#include "Technology.h"
#include "Interface.h"
#include "Game.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NewCity ////////////////////////////////////////////////////////////////////////////////////////////////////////
// creates a new city with the specified name and location ////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void NewCity(char civ, char name[], int x, int y, char emph) 
{
	char count = civilization[civ].citySize++;
	civilization[civ].city[count].x = x;
	civilization[civ].city[count].y = y;
	strcpy(civilization[civ].city[count].name, name);
	civilization[civ].city[count].population = 1;
	civilization[civ].city[count].foodStorage = 0;
	civilization[civ].city[count].food = 0;
	civilization[civ].city[count].trade = 0;
	civilization[civ].city[count].science = 0;
	civilization[civ].city[count].production = 0;
	civilization[civ].city[count].resourceEmph = emph;
	
	civilization[civ].city[count].underProduction = -1;
	civilization[civ].city[count].productionCurr = 0;
	
	char i;
	for(i = 0; i < BUILDING_NUMBER/8+1; i++)
		civilization[civ].city[count].buildings[i] = 0;
	if(count == 0) SetBit(civilization[civ].city[count].buildings, 0);
	
	ImproveCityTerrain(civ, count);
	CalculateResources(civ, count);
	CalculateHappiness(civ, count);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GrowCity ////////////////////////////////////////////////////////////////////////////////////////////////////////
// increases the size of the city ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void GrowCity(char civ, char city)
{
	civilization[civ].city[city].population++;
	CalculateResources(civ, city);
	CalculateHappiness(civ, city);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// RemoveCity /////////////////////////////////////////////////////////////////////////////////////////////////////
// removes a city from the array //////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RemoveCity(char index, char civ)
{
	char i;
	for(i = index; i < civilization[civ].citySize-1; i++)
		civilization[civ].city[i] = civilization[civ].city[i+1];
	civilization[civ].citySize--;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BuildCity //////////////////////////////////////////////////////////////////////////////////////////////////////
// builds a city, removes the settler /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool BuildCity(char index, char civ)
{
	if(civilization[civ].citySize == CITY_NUMBER) return 0;
	
	HANDLE dlg = H_NULL;
	char *name = &game.city_names[civilization[civ].type*240 + 16*civilization[civ].currentName++];
	
	if(civ == 0)
	{
		if(GetName(name))
		{
	    NewCity(civ, name, civilization[civ].unit[index].x, civilization[civ].unit[index].y, 0);
			RemoveUnit(index, civ);
			ChangeProductionWindow(0, civilization[0].citySize-1);
		}
	}
	else
	{
    NewCity(civ, name, civilization[civ].unit[index].x, civilization[civ].unit[index].y, 0);
		RemoveUnit(index, civ);
	}
  return 1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetCityIndex ///////////////////////////////////////////////////////////////////////////////////////////////////
// it searched for a city at the cursor, -1 if nothing found //////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char GetCityIndex(char civ)
{
	char index = -1, loop;
	
	for(loop = 0; loop < civilization[civ].citySize; loop++)
	{		
		if ((civilization[civ].city[loop].x - 5*game.qX) == game.cursor[0] && 
		    (civilization[civ].city[loop].y - 3*game.qY) == game.cursor[1])
    {
			index = loop;
			break;
    }
	}
	
	return index;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IsCityAt ///////////////////////////////////////////////////////////////////////////////////////////////////////
// determines if there is a city at a position ////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool IsCityAt(char civ, int x, int y)
{
	char i;
	for(i = 0; i < civilization[civ].citySize; i++)
	{		
		if (civilization[civ].city[i].x == x && 
				civilization[civ].city[i].y == y)
    {
			return 1;
    }
	}
	
	return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ActionCity /////////////////////////////////////////////////////////////////////////////////////////////////////
// determines what a certain city's stats are /////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ActionCity(char num) 
{
	char y = 0;
	bool update = 0;
	
	DrawStr (111, 60, "Zoom to city", A_NORMAL);
	Highlight2(111, 57 + 10 * y, 49);
	
	int key = 0;
	while(key != KEY_ENTER && key != KEY_ESC)
		key = ngetchx();
		
	if(key == KEY_ENTER)
	{
		ViewCity(0, num);
		update = 1;
	}
	
	return update;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ViewCity ///////////////////////////////////////////////////////////////////////////////////////////////////////
// provides a screen to view a city and its details ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ViewCity(char civ, char index) 
{
	int key = 0;
	bool newScreen = 1;
	
	char title[20], food[20], storage[20], production[20], trade[20], science[20], luxuries[20], turns[30];
	
	while(key != KEY_ESC)
	{
		key = 0;
		
		//clears & draws new screen
		if(newScreen)
		{
			newScreen = 0;
			ClrScr();
	
			sprintf(title, "%s   [%d]", civilization[civ].city[index].name, civilization[civ].city[index].population);
			sprintf(food, "Food: %d", civilization[civ].city[index].food);
			sprintf(storage, "Storage: %d / %d", civilization[civ].city[index].foodStorage, 10 * (civilization[civ].city[index].population+1));
			sprintf(production, "Production: %d", civilization[civ].city[index].production);
			
			int tradePercent = 100 - civilization[civ].sciencePercent - civilization[civ].luxuryPercent;
			sprintf(trade, "Trade (%d%%): %d", tradePercent, civilization[civ].city[index].trade);
			sprintf(science, "Science (%d%%): %d", civilization[civ].sciencePercent, civilization[civ].city[index].science);
			sprintf(luxuries, "Luxuries (%d%%): %d", civilization[civ].luxuryPercent, civilization[civ].city[index].luxury);
			
			//title
			DrawStr(5,  5, title, A_NORMAL);
			DrawLine(3, 11, 70, 11, A_NORMAL);
			
			//production
			DrawStr(5, 15, "Under Production:", A_NORMAL);
			if(civilization[civ].city[index].underProduction != -1)
			{
				if(civilization[civ].city[index].underProdType == 1)
				{
					DrawStr(8, 23, _units[civilization[civ].city[index].underProduction].name, A_NORMAL);
					int per = 100 * civilization[civ].city[index].productionCurr / _units[civilization[civ].city[index].underProduction].cost;
					int tur;
					if(civilization[civ].city[index].production == 0) tur = 999;
					else tur = (_units[civilization[civ].city[index].underProduction].cost - civilization[civ].city[index].productionCurr) / civilization[civ].city[index].production + 1;
					sprintf(turns, "%d%% done. %d Turns Left.", per, tur);
				}
				else
				{
					DrawStr(8, 23, buildings[civilization[civ].city[index].underProduction].name, A_NORMAL);
					int per = 100 * civilization[civ].city[index].productionCurr / buildings[civilization[civ].city[index].underProduction].cost;
					int tur;
					if(civilization[civ].city[index].production == 0) tur = 999;
					else tur = (buildings[civilization[civ].city[index].underProduction].cost - civilization[civ].city[index].productionCurr) / civilization[civ].city[index].production + 1;
					sprintf(turns, "%d%% done. %d Turns Left.", per, tur);
				}
					
				DrawStr(8, 31, turns, A_NORMAL);
			}
			else DrawStr(8, 23, "Nothing", A_NORMAL);
			
			//resources
			char resor[30];
			sprintf(resor, "Resources (%s)", resource_emph_name[civilization[civ].city[index].resourceEmph]);
			DrawStr(5, 43, resor, A_NORMAL);
			DrawStr(8, 51, food, A_NORMAL);
			DrawStr(8, 59, storage, A_NORMAL);
			DrawStr(8, 67, production, A_NORMAL);
			DrawStr(8, 75, trade, A_NORMAL);
			DrawStr(8, 83, science, A_NORMAL);
			DrawStr(75, 83, luxuries, A_NORMAL);
			
			//city view
			DrawLine(100,  5, 130,  5, A_NORMAL);
			DrawLine(90,  15, 140, 15, A_NORMAL);
			DrawLine(90,  25, 140, 25, A_NORMAL);
			DrawLine(90,  35, 140, 35, A_NORMAL);
			DrawLine(90,  45, 140, 45, A_NORMAL);
			DrawLine(100, 55, 130, 55, A_NORMAL);
			
			DrawLine(90,  15, 90,  45, A_NORMAL);
			DrawLine(100,  5, 100, 55, A_NORMAL);
			DrawLine(110,  5, 110, 55, A_NORMAL);
			DrawLine(120,  5, 120, 55, A_NORMAL);
			DrawLine(130,  5, 130, 55, A_NORMAL);
			DrawLine(140, 15, 140, 45, A_NORMAL);
			
			Sprite16(87, 58, SPRITE_HEIGHT, hFace, LCD_MEM, SPRT_OR);
			Sprite16(112, 58, SPRITE_HEIGHT, cFace, LCD_MEM, SPRT_OR);
			Sprite16(137, 58, SPRITE_HEIGHT, uFace, LCD_MEM, SPRT_OR);
			
			char hap[5], con[5], unh[5];
			sprintf(hap, "%d", civilization[civ].city[index].happy);
			sprintf(con, "%d", civilization[civ].city[index].content);
			sprintf(unh, "%d", civilization[civ].city[index].unhappy);
			
			DrawStr(88,  69, hap, A_NORMAL);
			DrawStr(113, 69, con, A_NORMAL);
			DrawStr(138, 69, unh, A_NORMAL);
			
			//draw terrain
			char x = civilization[civ].city[index].x, y = civilization[civ].city[index].y, i, j;
			for(i = 0; i < 5; i++)
			for(j = 0; j < 5; j++)
			{
			  //not corners
				if(!(i == 0 && (j == 0 || j == 4)) &&
				   !(i == 4 && (j == 0 || j == 4)) &&
				   !(i == 2 && j == 2))
				{
					DrawMapSquare2(civ, i, j, x, y);
				}
			}
			
			//draw city
			DrawCity2();
			
			//tabs
			DrawStr(0, 93, "Prev", A_NORMAL);
			DrawStr(29, 93, "Units", A_NORMAL);
			DrawStr(58, 93, "Production", A_NORMAL);
			DrawStr(104, 93, "Buildings", A_NORMAL);
			DrawStr(143, 93, "Next", A_NORMAL);
			
			//lines for tabs
			DrawLine(0, 91, 18, 91, A_NORMAL);
			DrawLine(27, 91, 47, 91, A_NORMAL);
			DrawLine(56, 91, 97, 91, A_NORMAL);
			DrawLine(102, 91, 136, 91, A_NORMAL);
			DrawLine(141, 91, 160, 91, A_NORMAL);
			
			DrawLine(18, 91, 18, 99, A_NORMAL);
			DrawLine(27, 91, 27, 99, A_NORMAL);
			DrawLine(47, 91, 47, 99, A_NORMAL);
			DrawLine(56, 91, 56, 99, A_NORMAL);
			DrawLine(97, 91, 97, 99, A_NORMAL);
			DrawLine(102, 91, 102, 99, A_NORMAL);
			DrawLine(136, 91, 136, 99, A_NORMAL);
			DrawLine(141, 91, 141, 99, A_NORMAL);
		}
		
		//wait for next key
		key = ngetchx();
		
		//changing current selected unit
		if(key == KEY_F1 || key == KEY_LEFT) // prev
		{
			if(--index < 0) index = civilization[civ].citySize - 1;
			newScreen = 1;
		}
		else if(key == KEY_F2) // units
		{
			newScreen = ShowCityUnits(civ, index);
		}
		else if(key == KEY_F3) // production
		{
			ShowProductionWindow(civ, index);
			newScreen = 1;
		}
		else if(key == KEY_F4) // buildings
		{
			
		}
		else if(key == KEY_F5 || key == KEY_RIGHT) // next
		{
			if(++index > civilization[civ].citySize - 1) index = 0;
			newScreen = 1;
		}
	}
	
	ClrScr();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ShowCityUnits //////////////////////////////////////////////////////////////////////////////////////////////////
// shows all the units in a city //////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ShowCityUnits(char civ, char city)
{
	char units[UNIT_NUMBER];
	char num = GetUnitIndicesAt(units, civilization[civ].city[city].x, civilization[civ].city[city].y);
	char i = 0, loop;
	char firstI = 0;
	
	char max = min(13, num);
	if(max == 0) return 0;
	
	char health[20], move[20], atk[20], def[20];
	
	int key = 0;
	while(key != KEY_ESC)
	{
		ClrScr();
		
		// Draws a box for units
		DrawLine(2,2,156,2, A_NORMAL);
		DrawLine(2,14,156,14, A_NORMAL);
		DrawLine(2,2,2,14, A_NORMAL);
		DrawLine(156,2,156,14, A_NORMAL);	
		
		for(loop = 0; loop < max; loop++)
			Sprite16(4 + loop*11, 4, SPRITE_HEIGHT, &game.unitpic[_units[civilization[civ].unit[units[loop]].type].pic*SPRITE_HEIGHT], LCD_MEM, SPRT_OR);
		
		char d = 11 * i;
		
		// Draw Box
		DrawLine( 3+d,  3, 13+d,  3, A_NORMAL);
		DrawLine( 3+d, 13, 13+d, 13, A_NORMAL);
		DrawLine( 3+d,  3,  3+d, 13, A_NORMAL);
		DrawLine(13+d,  3, 13+d, 13, A_NORMAL);
		
		sprintf(health, "Health: %d / %d", civilization[civ].unit[units[i+firstI]].hpCURR, civilization[civ].unit[units[i+firstI]].hpFULL);
		sprintf(move, "Movement: %d / %d", civilization[civ].unit[units[i+firstI]].moveCURR, _units[civilization[civ].unit[units[i+firstI]].type].moveMAX);
		
		sprintf(atk, "Attack: %d", _units[civilization[civ].unit[units[i+firstI]].type].atk);
		sprintf(def, "Defense: %d", _units[civilization[civ].unit[units[i+firstI]].type].def);
		
		FontSetSys(F_8x10);
		DrawStr(0, 20, _units[civilization[civ].unit[units[i+firstI]].type].name, A_NORMAL);
		
		FontSetSys(F_4x6);
		DrawStr(0, 35, health, A_NORMAL);
		DrawStr(0, 45, move, A_NORMAL);
		if(civilization[civ].unit[units[i+firstI]].isFortified) DrawStr(0, 55, "Fortified", A_NORMAL);
		
		DrawStr(70, 35, atk, A_NORMAL);
		DrawStr(70, 45, def, A_NORMAL);
		
		// Get Key
		key = ngetchx();
		
		if(key == KEY_LEFT)
		{
			if(--i < 0) i = max-1;
		}
		else if(key == KEY_RIGHT)
		{
			if(++i > max-1) i = 0;
		}
	}
	return 1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ShowProductionWindow ///////////////////////////////////////////////////////////////////////////////////////////
// allows for change in production & resource allocation //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ShowProductionWindow(char civ, char city)
{
	DrawLine( 20, 20, 130, 20, A_NORMAL);
	DrawLine( 20, 80, 130, 80, A_NORMAL);
	DrawLine( 20, 20,  20, 80, A_NORMAL);
	DrawLine(130, 20, 130, 80, A_NORMAL);
	     Clr( 21, 21, 129, 79);
	DrawLine( 20, 30, 130, 30, A_NORMAL);
	
	FontSetSys(F_6x8);
	DrawStr(22, 22, "Production", A_NORMAL);
	FontSetSys(F_4x6);
	
	char turns[30];
	DrawStr(22, 36, "Under Production:", A_NORMAL);
	if(civilization[civ].city[city].underProduction != -1)
	{
		if(civilization[civ].city[city].underProdType == 1)
		{
			DrawStr(25, 44, _units[civilization[civ].city[city].underProduction].name, A_NORMAL);
			int per = 100 * civilization[civ].city[city].productionCurr / _units[civilization[civ].city[city].underProduction].cost;
			int tur;
			if(civilization[civ].city[city].production == 0) tur = 999;
			else tur = (_units[civilization[civ].city[city].underProduction].cost - civilization[civ].city[city].productionCurr) / civilization[civ].city[city].production + 1;
			sprintf(turns, "%d%% done. %d Turns Left.", per, tur);
		}
		else
		{
			DrawStr(25, 44, buildings[civilization[civ].city[city].underProduction].name, A_NORMAL);
			int per = 100 * civilization[civ].city[city].productionCurr / buildings[civilization[civ].city[city].underProduction].cost;
			int tur;
			if(civilization[civ].city[city].production == 0) tur = 999;
			else tur = (buildings[civilization[civ].city[city].underProduction].cost - civilization[civ].city[city].productionCurr) / civilization[civ].city[city].production + 1;
			sprintf(turns, "%d%% done. %d Turns Left.", per, tur);
		}
			
		DrawStr(25, 52, turns, A_NORMAL);
	}
	else DrawStr(25, 44, "Nothing", A_NORMAL);
	DrawStr(110, 44, "[F1]", A_NORMAL);
	
	DrawStr(22, 62, "Resource Emphasis:", A_NORMAL);
	DrawStr(25, 70, resource_emph_name[civilization[civ].city[city].resourceEmph], A_NORMAL);
	DrawStr(110, 70, "[F2]", A_NORMAL);
	
	int key = 0;
	while(key != KEY_ESC && key != KEY_F1 && key != KEY_F2) key = ngetchx();
	
	if(key == KEY_F1)	
		ChangeProductionWindow(civ, city);
	else if(key == KEY_F2)
	{
		DrawLine(55, 34, 95, 34, A_NORMAL);
		DrawLine(55, 76, 95, 76, A_NORMAL);
		DrawLine(55, 34, 55, 76, A_NORMAL);
		DrawLine(95, 34, 95, 76, A_NORMAL);
		Clr(56, 35, 94, 75);
		
		DrawStr(60, 38, "Balanced", A_NORMAL);
		DrawStr(67, 48, "Food", A_NORMAL);
		DrawStr(63, 58, "Shields", A_NORMAL);
		DrawStr(65, 68, "Trade", A_NORMAL);
		
		int key2 = 0;
		char j = 0;
		
		while(key2 != KEY_ENTER && key2 != KEY_ESC)
		{
			Highlight2(56, 35+j*10, 38);
			key2 = ngetchx();
			Highlight2(56, 35+j*10, 38);
			
			if(key2 == KEY_DOWN)
			{
				if(++j > 3) j = 0;
			}
			else if(key2 == KEY_UP)
			{
				if(--j < 0) j = 3;
			}
			else if(key2 == KEY_ENTER)
			{
				civilization[civ].city[city].resourceEmph = j;
				CalculateResources(civ, city);
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ChangeProductionWindow /////////////////////////////////////////////////////////////////////////////////////////
// allows for change in production ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChangeProductionWindow(char civ, char city)
{
	ClrScr();
	
	DrawLine(69, 0, 69, 99, A_NORMAL);

	// Calculates number of units
	char unitI[11];
	char unitISize = GetAvailableUnits(civ, unitI);

	// Calculates number of buildings
	char buildingI[50];
	char buildingISize = GetAvailableBuildings(civ, city, buildingI);

	// Gets the index chosen from player
	char k = Menu(unitI, unitISize, buildingI, city, unitISize+buildingISize);
	
	if(k != -1)
	{
		if(k < unitISize) 
		{
			civilization[civ].city[city].underProdType = 1;
			civilization[civ].city[city].underProduction = unitI[k];
		}
		else
		{              
			civilization[civ].city[city].underProdType = 2;
			civilization[civ].city[city].underProduction = buildingI[k - unitISize];
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rounding ///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int roundFtoI(float x) 
{
	int x2 = (int)x;
	if(x < 1 && x >= 0.5)
		return 1;
	if(x2 != 0 && (float)(x - x2) / (float)(x2) >= 0.5) 
		return (x2)+1; 
	else return (x2); 
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CalculateTrade /////////////////////////////////////////////////////////////////////////////////////////////////
// uses the trade to calculate luxury, science, and trade /////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CalculateTrade(char civ, char city)
{

	civilization[civ].city[city].luxury = roundFtoI((float)civilization[civ].luxuryPercent * (float)civilization[civ].city[city].trade / 100.0);
	civilization[civ].city[city].science = roundFtoI((float)civilization[civ].sciencePercent * (float)civilization[civ].city[city].trade / 100.0);
	civilization[civ].city[city].trade -= (civilization[civ].city[city].luxury + civilization[civ].city[city].science); 
}

#define Swap(X,Y) {tmp[0] = X[0];tmp[1] = X[1];tmp[2] = X[2]; X[0] = Y[0];  X[1] = Y[1];  X[2] = Y[2]; Y[0] = tmp[0];Y[1] = tmp[1];Y[2] = tmp[2];}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CalculateResources /////////////////////////////////////////////////////////////////////////////////////////////
// calculates the resources based off of resource emphasis ////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CalculateResources(char civ, char city)
{
	char res[20][3];
	char k = 0;
	char emph = civilization[civ].city[city].resourceEmph;;
	
	char x = civilization[civ].city[city].x, y = civilization[civ].city[city].y, i, j;
	for(i = 0; i < 5; i++)
	for(j = 0; j < 5; j++)
	{
	  //not corners
		if(!(i == 0 && (j == 0 || j == 4)) &&
		   !(i == 4 && (j == 0 || j == 4)) &&
		   !(i == 2 && j == 2))
		{
			GetResources(i, j, x, y, res[k++], emph, civ, city);
		}
	}
	
	// sort by emphasis	
	char tmp[3];
	char size = 20;
	
	for (i=0; i<size-1; i++)
  for (j=0; j<size-1-i; j++)
  {
  	if(res[j][0] == res[j+1][0])
  	{
   		if(res[j][1] == res[j+1][1])
  		{
  			if(res[j][2] < res[j+1][2])
  				Swap(res[j], res[j+1])
  		}
  		else if(res[j][1] < res[j+1][1])
   			Swap(res[j], res[j+1])
  	}
 		else if(res[j][0] < res[j+1][0])
  		Swap(res[j], res[j+1])
  }
  
  GetResources(2, 2, x, y, tmp, 0, civ, city);
  
  civilization[civ].city[city].food = tmp[0];
  civilization[civ].city[city].production = tmp[1];
  civilization[civ].city[city].trade = tmp[2];
  
  for(i = 0; i < civilization[0].city[city].population; i++)
  {
  	if(emph < 2)
  	{
		  civilization[civ].city[city].food += res[i][0];
		  civilization[civ].city[city].production += res[i][1];
		  civilization[civ].city[city].trade += res[i][2];
  	}
  	else if(emph == 2)
  	{
		  civilization[civ].city[city].food += res[i][1];
		  civilization[civ].city[city].production += res[i][0];
		  civilization[civ].city[city].trade += res[i][2];
  	}
  	else
  	{
		  civilization[civ].city[city].food += res[i][1];
		  civilization[civ].city[city].production += res[i][2];
		  civilization[civ].city[city].trade += res[i][0];
  	}
  }
  
  CalculateTrade(civ, city);
  
  float sci_mult = 1.0;
  float tra_mult = 1.0;
  float pro_mult = 1.0;
  
  if(GetBit(civilization[civ].city[city].buildings, 5)) sci_mult += 0.5;
  if(GetBit(civilization[civ].city[city].buildings, 11)) sci_mult += 0.5;
  if(GetBit(civilization[civ].city[city].buildings, 25)
  						 || buildings[65].isConstructed == civ) sci_mult += 0.5;
  if(GetBit(civilization[civ].city[city].buildings, 50)) sci_mult += 0.5;
  if(GetBit(civilization[civ].city[city].buildings, 55)) sci_mult += 1.0;
  
  if(GetBit(civilization[civ].city[city].buildings, 4)) tra_mult += 0.5;
  if(GetBit(civilization[civ].city[city].buildings, 9)) tra_mult += 0.5;
  if(GetBit(civilization[civ].city[city].buildings, 22)) tra_mult += 0.5;
  
  if(GetBit(civilization[civ].city[city].buildings, 14)) pro_mult += 0.5;
  if(GetBit(civilization[civ].city[city].buildings, 15)) pro_mult += 0.5;
  if(civ == buildings[61].isConstructed ||
  	 GetBit(civilization[civ].city[city].buildings, 18) ||
  	 GetBit(civilization[civ].city[city].buildings, 19) ||
  	 GetBit(civilization[civ].city[city].buildings, 28) ||
  	 GetBit(civilization[civ].city[city].buildings, 20)) pro_mult += 0.5;

  civilization[civ].city[city].production *= pro_mult;
  civilization[civ].city[city].trade *= tra_mult;
  civilization[civ].city[city].luxury *= tra_mult;
  civilization[civ].city[city].science *= sci_mult;
  
  if(civ == buildings[56].isConstructed)
  {
	  for(i = 0; i < BUILDING_NUMBER; i++)
	  {
	  	if(GetBit(civilization[civ].city[city].buildings, i) && buildings[i].upkeep > 1)
	  		civilization[civ].city[city].trade -= buildings[i].upkeep;
	  }
  }
  else
  {
	  for(i = 0; i < BUILDING_NUMBER; i++)
	  {
	  	if(GetBit(civilization[civ].city[city].buildings, i))
	  		civilization[civ].city[city].trade -= buildings[i].upkeep;
	  }
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CalculateHappiness /////////////////////////////////////////////////////////////////////////////////////////////
// calculates the happiness of the people in the city /////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CalculateHappiness(char civ, char city)
{
	char pop = civilization[civ].city[city].population;
	char unhappy = 0;
	if(pop > 7)	unhappy = pop - 7;
	
	civilization[civ].city[city].happy = 0;
	civilization[civ].city[city].content = pop - unhappy;
	civilization[civ].city[city].unhappy = unhappy;
	
	// luxuries
	
	// buildings
	if(GetBit(civilization[civ].city[city].buildings, 3))
	{
		char num;
		if(civ == buildings[44].isConstructed) num = 4;
		else																	 num = 2;
		
		if(civilization[civ].city[city].unhappy < num)
		{
			civilization[civ].city[city].content += civilization[civ].city[city].unhappy;
			civilization[civ].city[city].unhappy = 0;
		}
		else
		{
			civilization[civ].city[city].content += num;
			civilization[civ].city[city].unhappy -= num;
		}
	}
	if(GetBit(civilization[civ].city[city].buildings, 10) || civ == buildings[49].isConstructed)
	{		
		if(civilization[civ].city[city].unhappy < 4)
		{
			civilization[civ].city[city].content += civilization[civ].city[city].unhappy;
			civilization[civ].city[city].unhappy = 0;
		}
		else
		{
			civilization[civ].city[city].content += 4;
			civilization[civ].city[city].unhappy -= 4;
		}
	}
	if(GetBit(civilization[civ].city[city].buildings, 13))
	{
		if(civilization[civ].city[city].unhappy < 3)
		{
			civilization[civ].city[city].content += civilization[civ].city[city].unhappy;
			civilization[civ].city[city].unhappy = 0;
		}
		else
		{
			civilization[civ].city[city].content += 3;
			civilization[civ].city[city].unhappy -= 3;
		}
	}
	if(GetBit(civilization[civ].city[city].buildings, 40))
	{
		if(civilization[civ].city[city].unhappy > 0)
		{ civilization[civ].city[city].unhappy--; civilization[civ].city[city].happy++; }
		else if(civilization[civ].city[city].content > 0) 
		{ civilization[civ].city[city].content--; civilization[civ].city[city].happy++; }		
	}
	if(civ == buildings[52].isConstructed)
	{
		civilization[civ].city[city].content += civilization[civ].city[city].unhappy;
		civilization[civ].city[city].unhappy = 0;
	}
	if(civ == buildings[54].isConstructed)
	{		
		if(civilization[civ].city[city].unhappy < 2)
		{
			civilization[civ].city[city].content += civilization[civ].city[city].unhappy;
			civilization[civ].city[city].unhappy = 0;
		}
		else
		{
			civilization[civ].city[city].content += 2;
			civilization[civ].city[city].unhappy -= 2;
		}
	}
	if(civ == buildings[66].isConstructed)
	{
		if(civilization[civ].city[city].unhappy > 0)
		{ civilization[civ].city[city].unhappy--; civilization[civ].city[city].happy++; }
		else if(civilization[civ].city[city].content > 0) 
		{ civilization[civ].city[city].content--; civilization[civ].city[city].happy++; }		
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AddBuildings ///////////////////////////////////////////////////////////////////////////////////////////////////
// sets the necessary values for the building arrays //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AddBuildings(char civ, char city)
{
	char p = civilization[civ].city[city].underProduction;
	
	civilization[civ].city[city].productionCurr -= buildings[p].cost;
	SetBit(civilization[civ].city[city].buildings, p);
	if(buildings[p].isWonder) buildings[p].isConstructed = civ;
	if(p == 57)
	{
		UpdateTechnology(civ);
		civilization[civ].researchAmount = (techs[civilization[civ].currentTechResearch].epoch + 1) * TECH_COST;
		UpdateTechnology(civ);
		civilization[civ].researchAmount = (techs[civilization[civ].currentTechResearch].epoch + 1) * TECH_COST;
		UpdateTechnology(civ);
	}
	
	CalculateResources(civ, city);
	CalculateHappiness(civ, city);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AI_DoCities ////////////////////////////////////////////////////////////////////////////////////////////////////
// provides the ai control of the cities //////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AI_DoCities(char civ)
{
	
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AI_UpdateCityProduction ////////////////////////////////////////////////////////////////////////////////////////
// decides on new production for the city /////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AI_UpdateCityProduction(char civ, char city)
{
	char ai = civ-1;
	
	bool buildUnit = 0;
	bool buildMilit = 0;
	
	switch(aiCivs[ai].type)
	{
	case MILITARISTIC:
		buildUnit = (random(5) < 3);
		buildMilit = (random(3) < 2);
		break;
	case EXPANSIONIST:
		buildUnit = (random(5) < 3);
		buildMilit = (random(2) > 0);
		break;	
	case PERFECTIONIST:
		buildUnit = (random(5) > 2);
		buildMilit = (random(3) > 1);
		break;	
	case ISOLATIONIST:
		buildUnit = (random(5) > 2);
		buildMilit = (random(2) > 0);
		break;	
	}
	
	bool set = 0;
	bool triedBuilding = 0;
	bool triedUnit = 0;
	
	while(!set)
	{
		if(buildUnit)
		{
			// set production to unit
			civilization[civ].city[city].underProdType = 1;
			
			if(buildMilit)
			{
				// Calculates number of units
				char unitI[11];
				char unitISize = GetAvailableUnits(civ, unitI);
				
				/*char cur = -1, i;
				for(i = 0; i < unitISize; i++)
				{
					// TODO: total income, number of units
				}*/
				
				set = 1;
				civilization[civ].city[city].underProduction = unitI[random(unitISize-1)+1];
			}
			else 
			{
				set = 1;
				civilization[civ].city[city].underProduction = 0;
			}
		}
		else
		{
			// set production to buildings
			civilization[civ].city[city].underProdType = 2;
			
			// Calculates number of buildings
			char buildingI[50];
			char buildingISize = GetAvailableBuildings(civ, city, buildingI);
			
			if(buildMilit)
			{
				char cur = -1, i = 0;
				while(i < buildingISize && cur == -1)
				{
					if(buildings[buildingI[i]].isMilitaristic) cur = buildingI[i];
					i++;
				}
				
				if(cur == -1)
				{
					i = 0;
					while(i < buildingISize && cur == -1)
					{
						if(buildingI[i] !=  0 && 
							 buildingI[i] != 37 &&
							 buildingI[i] != 38  ) cur = buildingI[i];
						i++;
					}
				}
				
				if(cur == -1)
				{
					if(triedUnit)
					{
						set = 1;
						civilization[civ].city[city].underProduction = 37 + random(2);
					}
					else
					{
						triedBuilding = 1;
						buildUnit = 1;
					}
				}
				else
				{
					set = 1;
					civilization[civ].city[city].underProduction = cur;
				}
			}
			else
			{
				char cur = -1, i = 0;
				while(i < buildingISize && cur == -1)
				{
					if(!buildings[buildingI[i]].isMilitaristic && 
							buildingI[i] !=  0 && 
							buildingI[i] != 37 &&
							buildingI[i] != 38  ) cur = buildingI[i];
					i++;
				}
				
				if(cur == -1)
				{
					i = 0;
					while(i < buildingISize && cur == -1)
					{
						if(buildings[buildingI[i]].isMilitaristic) cur = buildingI[i];
						i++;
					}
				}
				
				if(cur == -1)
				{
					if(triedUnit)
					{
						set = 1;
						civilization[civ].city[city].underProduction = 37 + random(2);
					}
					else
					{
						triedBuilding = 1;
						buildUnit = 1;
					}
				}
				else
				{
					set = 1;
					civilization[civ].city[city].underProduction = cur;
				}
			}
		}
	}
}




